/**
 * Created by yuming619 on 2016/10/21.
 */
//rem适配手机大小�
new function () {
    var _self = this;
    _self.width = 640;//�
    _self.fontSize = 100;//
    _self.widthProportion = function () {
        var p = (document.body && document.body.clientWidth || document.getElementsByTagName("html")[0].offsetWidth) / _self.width;
        //return p > 1 ? 1 : p < 0.5 ? 0.5 : p;
        return p;
    };
    _self.changePage = function () {
        document.getElementsByTagName("html")[0].setAttribute("style", "font-size:" + _self.widthProportion() * _self.fontSize + "px !important");
    }
    _self.changePage();
    window.addEventListener('resize', function () {
        _self.changePage();
    }, false);
};
/*底部按钮切换*/
function EnterChange(x) {
    if (x == 1) {
        document.getElementById("img1").src = "images/redmenu.png"
        document.getElementById("img2").src = "images/heiorder.png"
        document.getElementById("img3").src = "images/heilogin.png"
        $("#exchange1").find("span").addClass("bot_focus");
        $("#exchange2").find("span").removeClass("bot_focus");
        $("#exchange3").find("span").removeClass("bot_focus");
    } else if (x == 2) {
        document.getElementById("img1").src = "images/heimenu.png"
        document.getElementById("img2").src = "images/redorder.png"
        document.getElementById("img3").src = "images/heilogin.png"
        $("#exchange1").find("span").removeClass("bot_focus");
        $("#exchange2").find("span").addClass("bot_focus");
        $("#exchange3").find("span").removeClass("bot_focus");
    } else if (x == 3) {
        document.getElementById("img1").src = "images/heimenu.png"
        document.getElementById("img2").src = "images/heiorder.png"
        document.getElementById("img3").src = "images/redlogin.png"
        $("#exchange1").find("span").removeClass("bot_focus");
        $("#exchange2").find("span").removeClass("bot_focus");
        $("#exchange3").find("span").addClass("bot_focus");
    }
}

//侧边栏滑动页面
function sideload() {
    var homepages = $("#homepages");
    var homepagesClass = document.querySelector('.homepages');
    var topAddressSearch = document.querySelector('.top_address_search');
    var homeTop = $('.home-top');
    var searchHeight = 50, initialOpacity = 0, mostOpacity = 0.9;
    var side = document.getElementById('side');
    side.addEventListener('touchstart', touch, false);
    function touch(event) {
        var event = event || window.event;
        switch (event.type) {
            case "touchstart":
                startTouch();
                break;
            case "touchmove":
                moveTouch();
                break;
            case "touchend":
                endTouch();
                break;
        }

    }

    function startTouch() {
        side.addEventListener('touchmove', touch, false);
        side.addEventListener('touchend', touch, false);
    }

    function moveTouch(e) {
        topStyle();
    }

    function endTouch() {
        side.removeEventListener('touchend', touch, false);
    }

    homepagesClass.addEventListener('scroll', topStyle, false);
    function topStyle() {
        var homepagesTop = homepagesClass.scrollTop;
        if (homepagesTop < searchHeight) {
            topAddressSearch.style.backgroundColor = 'rgba(248,63,60,' + initialOpacity + ')';
            $(".menu_head").css({display: "block"});
            $(".shop_search").css({width: "60%"})
        }
        else if (homepagesTop < homeTop[0].offsetHeight) {
            topAddressSearch.style.backgroundColor = 'rgba(248,63,60,' + ((homepagesTop - searchHeight) / (homeTop[0].offsetHeight)) + ')';
            $(".menu_head").css({display: "block"});
            $(".shop_search").css({width: "60%"})
        } else {
            topAddressSearch.style.backgroundColor = 'rgba(248,63,60,' + mostOpacity + ')';
            $(".menu_head").css({display: "none"});
            $(".shop_search").css({width: "100%"})
        }
    }
}

//接口同步服务器断点动画
function toastHtml(html, show) {
    var float_element = $(".c-float-popWrap")
    if (show == true) {
        var hei = $(window).height() / 2
        var top = $(document).scrollTop() + hei - 30;
        float_element.css({"top": top + "px"});
        $(".warnMsg").html(html)
        float_element.show();
    } else {
        float_element.fadeOut();
    }
}

//手机号码格式判断
function judgePhone(phone){
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0-9]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/;
    if (!phone||phone == '') {
        input_alert("手机号码不能为空！");
        return false;
    } else if (phone.length != 11) {
        input_alert("请输入有效的手机号码！");
        return false;
    } else if (!myreg.test(phone)) {
        input_alert("请输入有效的手机号码！");
        return false;
    }
    return true;
}
/*提示用户竖屏访问*/
(function rotate(){
    var orientation=window.orientation;
    if(orientation==90||orientation==-90){
        document.body.style.display='none';
        alert("请使用竖屏访问！");
    }
    window.onorientationchange=function(){
        document.body.style.display="block";
        rotate();
    };
})()
